#
# Copyright (c) 2024 Ingenic Semiconductor Co.,Ltd
# Author: Keven <keven.ywhan@ingenic.com>
#
# Find out source, build, and install directories
SDK_DIR = $(CURDIR)/..
BUILD_DIR = build

include $(SDK_DIR)/config.mk

######################################
# target
######################################
TARGET = spl

######################################
# building variables
######################################
# debug build?
DEBUG = 0

# optimization
OPT = -fpie -fno-builtin -fomit-frame-pointer -Wall -nostdlib -Werror-implicit-function-declaration

OBJECTS = \
	$(BUILD_DIR)/start.o \
	$(BUILD_DIR)/main_spl.o \
	$(BUILD_DIR)/drivers/uart.o \
	$(BUILD_DIR)/drivers/clk.o \
	$(BUILD_DIR)/drivers/time.o \
	$(BUILD_DIR)/drivers/ddr.o \
	$(BUILD_DIR)/drivers/sram.o \
	$(BUILD_DIR)/drivers/msc.o \
	$(BUILD_DIR)/drivers/sfc.o \
	$(BUILD_DIR)/drivers/usb.o \
	$(BUILD_DIR)/lib/memcpy.o \
	$(BUILD_DIR)/lib/memset.o \
	$(BUILD_DIR)/lib/strcmp.o \
	$(BUILD_DIR)/lib/strlen.o


#######################################
# binaries
#######################################
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)as
LD = $(GCC_PATH)/$(PREFIX)ld
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
AR = $(GCC_PATH)/$(PREFIX)ar
READELF = $(GCC_PATH)/$(PREFIX)readelf
OBJDUMP = $(GCC_PATH)/$(PREFIX)objdump
else
CC = $(PREFIX)gcc
AS = $(PREFIX)as
LD = $(PREFIX)ld
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
AR = $(PREFIX)ar
READELF = $(PREFIX)readelf
OBJDUMP = $(PREFIX)objdump
endif
BIN = $(CP) -O binary -S

#######################################
# CFLAGS
#######################################
# cpu
CPUFLAGS = -march=rv32imc -mabi=ilp32 -Wno-abi

# fpu
FPU =

# float-abi
FLOAT-ABI =

# mcu
#MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
MCU = $(CPUFLAGS) $(FPU) $(FLOAT-ABI)

# macros for gcc
# AS defines
AS_DEFS = -D_ASSEMBLER_ -D__ASSEMBLY__

# C defines
C_DEFS =

# AS includes
AS_INCLUDES = -Iinclude

# C includes
C_INCLUDES = -Iinclude

# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -static

CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -static

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2 -O0
else
CFLAGS += -O2
endif

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

ARFLAGS	= rcs

#######################################
# LDFLAGS
#######################################
LDSCRIPT = target.ld
# libraries
#LIBS = -lc -lm -lnosys
LIBDIR =
# LDFLAGS = -T$(LDSCRIPT)
# LDFLAGS = $(CFLAGS) -T$(LDSCRIPT) -Wl,--whole-archive -Wl,--no-whole-archive -fpie
LDFLAGS = -T$(LDSCRIPT)

#######################################
# OBJCFLAGS
#######################################
limit = 32768
# limit = $(shell printf "%d" $(CONFIG_SPL_MAX_SIZE))
OBJCFLAGS = --gap-fill=0xff --pad-to=$(limit)

# default action: build all
all: $(TARGET).elf $(TARGET)-pad.bin tmpfiles

#######################################
# build the application
#######################################

$(BUILD_DIR)/%.o: %.c  Makefile | $(BUILD_DIR)
	mkdir -p $(dir $@)
	$(CC) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/%.o: %.S  Makefile | $(BUILD_DIR)
	mkdir -p $(dir $@)
	$(CC) -c $(ASFLAGS) -o $@ $<

$(TARGET).elf: $(OBJECTS)
	$(LD) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(TARGET).bin: $(TARGET).elf
	$(BIN) $< $@

$(TARGET)-pad.bin: $(TARGET).bin
	$(CP) $(OBJCFLAGS) -I binary $< -O binary $@

$(BUILD_DIR):
	mkdir $@

.PHONY: clean

#######################################
# clean up
#######################################
clean:
	@rm -fR $(BUILD_DIR)
	@rm -rf $(TARGET).* *.map $(TARGET)-pad.bin .autoconf.mk

tmpfiles: $(TARGET).elf
	$(READELF) -a $< > $(TARGET).readelf
	$(OBJDUMP) -d $< > $(TARGET).dump
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

# *** EOF ***
